<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>        

<script type="text/javascript">	
	$(document).ready(function(){
		//초기에 화면에 뿌려줄 내용
		$("#total_gugunDiv").hide();
		$.ajax({
			type:"POST",
			url:"hopeSubjectList.do",
			dataType:"json",
			success:function(jsonData){	
				//시도 리스트 가져와서 뿌리기
				$("#total_sido option").remove();
				var op="<option value='false'>--</option>";
				$("#total_sido").append(op);
 				$.each(jsonData.sidoList,function(index, sidolist){	//jsonData.sidoList을 sidolist란 이름으로 사용
 					op="<option value="+sidolist.code_code+">"+sidolist.code_name+"</option>";
					$("#total_sido").append(op); 
				});

 				//희망과목 가져와서 뿌리기
				$("#total_hopesubject option").remove();
				op="<option value='false'>--</option>";
				$("#total_hopesubject").append(op);
 				$.each(jsonData.hopeSubjectList,function(index,hopesubjectlist){
 					op="<option value="+hopesubjectlist.code_code+">"+hopesubjectlist.code_name+"</option>";
					$("#total_hopesubject").append(op); 
				});
 				//성별 가져와서 뿌리기
				$("#total_gender option").remove();
				op="<option value='false'>--</option>";
				$("#total_gender").append(op);
 				$.each(jsonData.genderList,function(index, genderlist){
 					op="<option value="+genderlist.code_code+">"+genderlist.code_name+"</option>";
					$("#total_gender").append(op); 
				});
 				//학교학년 가져와서 뿌리기
				$("#total_grade option").remove();
				op="<option value='false'>--</option>";
				$("#total_grade").append(op);
 				$.each(jsonData.gradeList,function(index, gradelist){
 					op="<option value="+gradelist.code_code+">"+gradelist.code_name+"</option>";
					$("#total_grade").append(op); 
				});
			}//success
		});//ajax

		$("#total_sido").change(function(){
			total_sido=$(this).val();
			if(total_sido==false){//시도 값이 바뀌었는데 --을 선택한 경우
				$("#total_gugunDiv").hide();
			}else{						
				$("#total_gugunDiv").show();				
			}			
			$.ajax({
				type:"POST",
				url:"gugunList.do",
				data:"code_code="+total_sido,
				dataType:"json",
				success:function(jsonData){
	 				//구군정보 가져와서 뿌리기
					$("#total_gugun option").remove();
					var op="<option value='false'>--</option>";
					$("#total_gugun").append(op);
	 				$.each(jsonData,function(index, codemaster){
	 					op="<option value="+codemaster.code_code+">"+codemaster.code_name+"</option>";
						$("#total_gugun").append(op); 
					});
				}
			}); 
		});
		
		$("#searchForm").submit(function(){
			var total_sido=$("#total_sido").val();
			var total_gugun=$("#total_gugun").val();
			var total_name=$("#total_name").val();
			var total_grade=$("#total_grade").val();
			var total_gender=$("#total_gender").val();
			var total_hopesubject=$("#total_hopesubject").val();
			if(total_sido=="false" && total_gugun==null && total_grade=="false" && total_gender=="false" && total_hopesubject=="false" && total_name==""){
				alert("검색조건 선택없이 검색하시겠습니까?.");
			}else{	//조건에 따른 검색
				$.ajax({
					type:"POST",
					url:"getSearchStudentList.do",
					data:$("#searchForm").serialize(),
					dataType:"json",
					success:function(jsonData){
						alert("검색조건 성공!!");
					}
				});				
			}
			//confirm("전송하시겠습니까?"); 에서 확인을 누르면 전송한다.
			
			//추가 테스트 : total_name 로 접근하기 위해서는 jquery selector 를 활용
			// alert($("#userAddress").val()); // 정상작동 안됨
			//return confirm("total_sido : "+total_sido+"total_gugun : "+total_gugun+"total_name : "+total_name+"total_grade : "+total_grade+"total_gender : "+total_gender+", total_hopesubject : "+total_hopesubject+" 검색하시겠습니까?"); 
			//return false; //return false 는 object의 진행을 멈추게 한다.
		});		
	});
</script>

<form id=searchForm action="searchStudentList.do">
<table width="560"  cellpadding="2" cellspacing="0"><tr><td>
	<table>
		<tr>
			<td>희망지역</td>
			<td ><select id = "total_sido"></select></td>
			<td colspan="2"> <span id="total_gugunDiv" ><select id="total_gugun"></select></span></td>
			<td>이름</td><td><input type="text" id="total_name"></td>
		</tr>
		<tr>
			<td>학년</td><td><select id="total_grade"></select></td>		
			<td>성별</td><td><select id="total_gender"></select></td>
			<td>희망과목</td><td><select id="total_hopesubject"></select></td>	
			<td align="right"><input type="submit"  value="검색"> </td>		
		</tr>
	</table>
</td></tr></table>	
</form>
<table width="560" cellpadding="2" cellspacing="0"><tr align="right"><td>
총 학생 수 : ${requestScope.listVO.pagingBean.totalContent}
</td></tr></table>	
<table width="560"  border="1" cellpadding="2" cellspacing="0">
	<thead>
		<tr><td width="20%">이름</td>
			 <td width="20%">성별</td>
			 <td width="20%">희망지역</td>
			 <td width="20%">희망과목</td>
			 <td width="20%">학교</td>
		</tr>
	</thead>
	<tbody>
			<c:forEach var="mvo" items="${requestScope.listVO.list}">		
			<tr>
			    <td><a href="showContent.do?id=${mvo.id }">${mvo.name }</a></td>			
				<td>${mvo.gender}</td>
				<td>${mvo.hoperegion}</td>
				<td>${mvo.hopesubject}</td>
				<td>${mvo.grade}</td>
			</tr>
			</c:forEach>	
	</tbody>	
</table><br>
<table width="560"  cellpadding="2" cellspacing="0"><tr align="center"><td>
<c:set var="pb" value="${requestScope.listVO.pagingBean}"></c:set>
<c:if test="${pb.previousPageGroup}">
	<a href="studentListBasic.do?pageNo=${pb.startPageOfPageGroup-1 }">	Pre </a>
</c:if>	

<c:forEach var="pageNo" begin="${pb.startPageOfPageGroup }" 
										end="${pb.endPageOfPageGroup }">
<c:choose>
<c:when test="${pb.nowPage !=pageNo }" >
	<a href="studentListBasic.do?pageNo=${pageNo }" >${pageNo }</a>
</c:when>
<c:otherwise>
	${pageNo} 
</c:otherwise>
</c:choose>
</c:forEach>

<c:if test="${pb.nextPageGroup}">
	<a href="studentListBasic.do?pageNo=${pb.endPageOfPageGroup+1 }">Next </a>
</c:if>
</td></tr></table>	
<br>
